{% set per_page_size = 10 %}
{% set page_count = [0] %}
{% set op_name = [''] %}
{% set index = [0] %}

<style type="text/css">
.breakpage { page-break-after:always;}
</style>

{% macro item_td(index, page_count, td_left_color, td_style, td_top_color, zip, address, name, department, linkname, grade, customer, op_name, county_name) -%}
    {% set td_style = '' %}
    {% set td_top_color = '' %}
    {% set td_left_color = '' %}

    {% set page_break = index[0] % (per_page_size * 2) == 0 %}
    {% set last_op_name = op_name[0] %}
    {% if op_name.pop(),op_name.append(customer.op_name) %}{% endif %}

    <!-- 如果业务员名字不同则进行分页 -->
    {% if op_name[0] != last_op_name and last_op_name != '' %}
        {% set halving_line = true %}
        {% set page_break = true %}
        {% if index.pop() %}{% endif %}
        {% if index.append(0) %}{% endif %}
    {% else %}
        {% set halving_line = false %}
    {% endif %}

    {% if halving_line %}
      {% set td_top_color = 'border-top:1px solid #55555;' %}
      {% set td_left_color = 'border-left:1px solid #55555;' %}
    {% endif %}


    {% if page_break %}
        <!-- 如果有之前的业务员名字，则需要结束table -->
        {% if last_op_name %}
          <tr><td colspan="2" align="right">第{{page_count[0]}}页({{last_op_name}})</td></tr></table>
          </div><br /><div class="breakpage"></div>
        {% endif %}
      <div style="border: 0px solid #aaaaaa">
      <table width="100%" border="0" cellspacing="0" cellpadding="0">
        {% if page_count.append(page_count.pop() + 1) %}{% endif %}
    {% endif %}
    {% if index[0] % 2 == 0 %}
      {% if index[0] > 0 %}
          </tr>
      {% endif %}
      <tr>
      {% set td_style = "border-right:1px solid #eeeeee;" %}
    {% endif %}

      <td style="{{td_left_color}}{{td_style}}border-bottom:1px solid #eeeeee;padding-top:0px;{{td_top_color}} padding-bottom:0px;height: 90px">
      &nbsp;<span style="padding-left: 5px;">{{zip|default('',True)}}</span><br />
      &nbsp;<font style="font-family:STSong,宋体;font-size: 14px">
          {% if address %}
            {%  if county_name %}
                {% if address.find(county_name) >= 0 %}
                    {{address}}
                {% else %}
                    {{county_name}}{{address|default('',True)}}
                {% endif %}
            {% else %}
                {{address}}
            {% endif %}
          {% else %}
            {{county_name}}
          {% endif %}
      </font>
    <br />
    <h5 style="margin-top:3px; margin-bottom:0px;font-family:STHeiti,微软雅黑,黑体;font-size: 15px;">&nbsp;&nbsp;
      {{name|default('',True)}}</h5>
    <nobr><span style="margin-top:0px; margin-bottom:0px;font-family:楷体_GB2312,STKaiti,华文楷体;font-size: 16px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      {{department|default('',True)}} {{linkname|default('',True)}} (收)</span>
      </nobr>
      <span style="color:#eeeeee; font-size: 8px; float: right">{{grade}}</span>
      </td>
    {% if index.append(index.pop() + 1) %}{% endif %}
{%- endmacro %}

{% for customer in customers %}
    <!-- 取得需要邮寄的联系人 -->
    {% set contacts_rows = _cr.dictfetchall("SELECT rs.name, cc.department, rs.county_id, rs.street, rs.zip FROM company_contacts cc INNER JOIN res_partner rs ON cc.contacts_id=rs.id WHERE cc.is_inauguratory=TRUE AND cc.partner_id=%s AND (cc.subscription_type='%s')" % (customer.id, subscription_type)) %}
    <!-- 遍历需要打印的联系人 -->
    {% for contacts in contacts_rows %}
        {% set department = contacts.department %}
        {% set linkname = contacts.name %}
        {% set address = contacts.street %}
        {% set zip = contacts.zip %}
        {% set county_name = counties[contacts.county_id] %}
        {{ item_td(index, page_count, td_left_color, td_style, td_top_color, zip, address, customer.name, department, linkname, grades[customer.cust_grade], customer, op_name, county_name) }}
    {% endfor %}
{% endfor %}
<tr><td colspan="2" align="right">第{{page_count[0]}}页({{op_name[0]}})</td></tr></table>
</div>